﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Runtime;
using Obsidian.Utils;
using Oak.Model;
using Oak.Core.Data;
using Oak.Base;

namespace Oak.Core.Logic
{
    public class ActCategoryLG : OakLG<OakSession>
    {

        public ActCategoryLG(OakSession session) : base(session) { }

        public CoActCategoryInfo GetArtCatById(long catId, out string msg)
        {
            CoActCategoryInfo cat = OModel.GetByPk<CoActCategoryInfo>(catId);
            if (cat == null)
            {
                msg = "该分类信息不存在";
                return null;
            }
            msg = "";
            return cat;
        }

        public List<CoActCategoryInfo> GetActCatList(CoActCategoryQry qry, out string msg)
        {

            ActCategoryDA catDA = new ActCategoryDA();

            if (qry.Enabled.IsNull)
                qry.Enabled.Set(true);

            List<CoActCategoryInfo> list = catDA.SelectActCatList(qry);
            if (list == null)
            {
                string code = Logger.Error(new Exception("运行错误"));
                msg = "内部错误：" + code;
                return null;
            }

            if (qry.GetChildren.isTrue() && list.Count > 0)
            {
                CoActCategoryQry qryChildren = new CoActCategoryQry();
                foreach (CoActCategoryInfo cat in list)
                    qryChildren.Paths.Add(cat.Path.Value + cat.CategoryId.Value + ",");

                List<CoActCategoryInfo> listChildren = catDA.SelectActCatList(qryChildren);
                if (listChildren == null)
                {
                    string code = Logger.Error(new Exception("运行错误"));
                    msg = "内部错误：" + code;
                    return null;
                }
                list.AddRange(listChildren);
            }
            msg = "";
            return list;
        }


        public CoActCategoryInfo AddArtCat(CoActCategoryInfo cat, out string msg)
        {
            cat.Modified.Now();
            cat.Created.Now();
            cat.Enabled.Set(true);

            if (!VerfiyArtCat(cat, out msg))
                return null;

            ActCategoryDA actCatDA = new ActCategoryDA();

            try
            {
                cat = actCatDA.InsertArtCat(cat);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "保存信息错误：" + code;
                return null;
            }

            msg = "";
            return cat;
        }

        public CoActCategoryInfo UpdateArtCat(CoActCategoryInfo cat, out string msg)
        {
            if (cat.CategoryId.IsNull)
            {
                msg = "传入类别ID不能为空";
                return null;
            }

            CoActCategoryInfo dbCat = this.GetArtCatById(cat.CategoryId.Value, out msg);
            if (dbCat == null)
                return null;

            int origSortOrder = dbCat.SortOrder.Value;
            dbCat.ResetAssigned();

            if (!cat.CategoryName.IsNull)
                dbCat.CategoryName.Set(cat.CategoryName.Value);

            if (cat.Enabled.isTrue())
                dbCat.Enabled.Set(cat.Enabled.Value);

            if (!VerfiyArtCat(cat, out msg))
                return null;

            /*
            if (!cat.SortOrder.IsNull)
                dbCat.SortOrder.Set(cat.SortOrder.Value);
             * */

            ActCategoryDA catDA = new ActCategoryDA();

            try
            {
                cat = catDA.UpdateArtCat(cat);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "保存信息错误：" + code;
                return null;
            }

            msg = "";
            return cat;
        }

        public IList<CoActCategoryInfo> GetActCatPaths(IEnumerable<long> catIds, out string msg)
        {

            List<CoActCategoryInfo> list;
            try
            {
                list = ActCategoryDA.SelectNodesFullPath(catIds);
            }
            catch (Exception ex)
            {
                string code = Logger.Error(ex);
                msg = "发生系统错误:" + code;
                return null;
            }
            msg = "";
            return list;
        }

        public static bool VerfiyArtCat(CoActCategoryInfo cat, out string msg)
        {
            if (cat.CategoryName.IsNullOrEmpty)
            {
                msg = "类别名称不能为空！";
                return false;
            }

            msg = "";
            return true;
        }

    }
}
